module.exports = function (grunt) {

    var combineConfig = {
        name: 'widget/widgets',
        create: true,
        include: ["widget/calendar", "widget/datepicker", "widget/tree", "widget/table", "widget/dialog"]
    };

    grunt.initConfig({

        pkg: grunt.file.readJSON('package.json'),
        stylus: {
            compile: {
                files: [{
                    'css/main.css': 'css/main.styl'
                }]
            }
        },
        requirejs: {

            compile: {

                options: {

                    baseUrl: "js",
                    dir: 'js',
                    findNestedDependencies: true,
                    paths: {
                        jquery: 'empty:',
                        template: 'empty:'
                    },
                    mainConfigFile: 'js/main.js',
                    fileExclusionRegExp: 'lib|main|require',
                    removeCombined: true,
                    allowSourceOverwrites: true,
                    keepBuildDir: true,
                    modules: [combineConfig]
                }

            }

        },

        clean: {
            contents: ['js/*.txt', 'css/*.styl']
        },
        filerev: {
            options: {
                algorithm: 'md5',
                length: 8
            },
            assets: {
                files: [{
                    src: [
                        'js/lib/*.js',
                        'js/widget/*.js',
                        'js/page/*.js'
                    ]
                }]
            }
        },
        replace: {
            example: {
                src: ['js/main.js'],
                overwrite: true,
                replacements: [{
                    from: /paths[\s\S]+?\}/,
                    to: function (matchedWord) {
                        var str = [];
                        for (var item in grunt.filerev.summary) {
                            var key = JSON.parse(JSON.stringify(item).replace(/\\\\/g, '/')).replace(/(js\/)|(lib\/)|(\.js)/g, '');
                            var value = JSON.parse(JSON.stringify(grunt.filerev.summary[item]).replace(/\\\\/g, '/')).replace(/(js\/)|(\.js)/g, '');
                            str.push("\"" + key + "\":\"" + value + "\"");
                        }
                        return 'paths:{' + str.join(',') + "}";
                    }
                }, {
                    from: /bundles[\s\S]+?\}/,
                    to: function () {
                        return 'bundles:{'+JSON.stringify(combineConfig.name)+':'+JSON.stringify(combineConfig.include)+'}';
                    }
                }]
            }
        }

    });

    grunt.loadNpmTasks('grunt-contrib-stylus');
    grunt.loadNpmTasks('grunt-contrib-requirejs');
    grunt.loadNpmTasks('grunt-contrib-clean');
    grunt.loadNpmTasks('grunt-filerev');
    grunt.loadNpmTasks('grunt-text-replace');

    grunt.registerTask('default', ['stylus', 'requirejs', 'filerev', 'replace', 'clean']);
};